"""
@FileName：daily_exchange_rate_spider1.py\n
@Description：\n
@Author： 邹滨峻 罗阳\n
@Time：2024/4/21\n
@Copyright：©2019-2024 研发中心
"""
import scrapy
from lxml import etree
from datetime import datetime
from 爬虫.eastmoney.eastmoney.items import daily_exchange_rate

current_datetime = datetime.now()

class money_flow_daily_spider(scrapy.Spider):  # 继承scrapy.Spider类
    name = 'daily_exchange_rate'  # spider名称，为spider唯一标识
    allowed_domains = ['data.eastmoney.com']  # 允许此爬虫抓取的域的字符串的可选列表，指定一个列表可以抓取，其它就不会抓取了
    start_urls = ['https://quote.eastmoney.com/center/gridlist.html#forex_cross']  # 爬取起始页，当没有指定特定网址时，爬虫将开始抓取的网址列表
    #指定pipelines，如果不指定，则在settings中申明了多少，就用多少
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }
    def parse(self,response):
        item = daily_exchange_rate() #定义Item类的对象，用于保存一条数据
        html = etree.HTML(response.text)
        tr_list = html.xpath("//table[@id='table_wrapper-table']//tbody/tr")
        for tr in tr_list:
            cols = tr.xpath('.//td')
            item['code'] = cols[1].xpath('.//text()')
            item['name'] =cols[2].xpath('.//a//text()')  #代码
            item["large_net_inflow"] = cols[3].xpath('.//text()')  #名称
            item["change_amount"] = cols[4].xpath('.//text()')  #最新价
            item["change_percentage"] = cols[5].xpath('.//text()')  #跌涨额
            item["open_price"] = cols[6].xpath('.//text()')  #跌涨幅
            item["highest_price"] = cols[7].xpath('.//text()') #今开
            item['lowest_price'] = cols[8].xpath('.//text()')#最高
            item['previous_close_price'] = cols[9].xpath('./text()')#昨收
            item['create_date'] = current_datetime.strftime('%Y-%m-%d %H:%M:%S')
            yield item
